package com.example.service.impl;

import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.base.service.impl.BaseServiceImpl;
import com.example.common.utils.ApiResult;
import com.example.config.webscoket.WebSocket;
import com.example.dao.SuppliesRealinfoMapper;
import com.example.dao.SuppliesRealinfoWarningMapper;
import com.example.model.entity.Supplies;
import com.example.model.entity.SuppliesRealinfo;
import com.example.model.entity.SuppliesRealinfoWarning;
import com.example.model.query.SuppliesRealinfoQuery;
import com.example.service.SuppliesRealinfoService;
import com.example.service.SuppliesService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

@Service
@Slf4j
public class SuppliesRealinfoServiceImpl extends BaseServiceImpl<SuppliesRealinfoMapper, SuppliesRealinfo> implements SuppliesRealinfoService {

	@Autowired
	private SuppliesRealinfoMapper realinfoMapper;
	@Autowired
	private SuppliesService suppliesService;
	@Autowired
	private SuppliesRealinfoService suppliesRealinfoService;
	@Resource
	private WebSocket webSocket;
	@Autowired
	private SuppliesRealinfoWarningMapper realinfoWarningMapper;

//	private static final Logger logger = LoggerFactory.getLogger(SuppliesRealinfoServiceImpl.class);
	
	/**
	 * 序列
	 */
	@Override
	public Long seqAlways() {
		return realinfoMapper.seqAlways();
	}

	@Override
	public ApiResult<SuppliesRealinfoQuery> pageUserVoByQuery(SuppliesRealinfoQuery query) throws Exception{
		query.setOptimizeCountSql(false);
		Long current = query.getCurrent();
		query.setCurrent(1L);
		QueryWrapper<SuppliesRealinfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(SuppliesRealinfo::getIsDanger,1L).orderByDesc(SuppliesRealinfo::getCreateTime);
		Integer total = realinfoMapper.selectCount(queryWrapper);
		query.setTotal(total);
		query.setCurrent(current);
		realinfoMapper.selectPage(query,queryWrapper);
//		List list = query.getRecords();
//		for (int i = 0; i < list.size(); i++) {
//
//		}
		return new ApiResult<>(query);
	}

	/**
	 * socket上报数据处理
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public ApiResult<Boolean> saveRealinfo(String jsonStr) throws Exception {
		log.info("截取后的Str="+jsonStr);
		JSONObject json = JSONObject.parseObject(jsonStr);
		String dev_id = json.getString("dev_id");
		if(dev_id == null){
			log.info("dev_id不存在");
			return new ApiResult<>(Boolean.FALSE);
		}
		//查物资是否存在
		QueryWrapper<Supplies> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(Supplies::getSuppliesNum,dev_id)
				.eq(Supplies::getIsValid,1L);
		Supplies supplies = suppliesService.getOne(queryWrapper);
		SuppliesRealinfo info = new SuppliesRealinfo();
		if(supplies == null) {
			//新增物资
			supplies = new Supplies();
			supplies.setSuppliesNum(dev_id);
			supplies.setSuppliesName(dev_id);
			supplies.setTypeId(dev_id);
			supplies.setCreateTime(new Date());
			suppliesService.save(supplies);
		}
		//更新物资状态
		QueryWrapper<SuppliesRealinfo> suppliesRealinfoQueryWrapper = new QueryWrapper<>();
		suppliesRealinfoQueryWrapper.lambda().eq(SuppliesRealinfo::getSuppliesId,supplies.getSuppliesId())
				.eq(SuppliesRealinfo::getIsNew,1L);
		List<SuppliesRealinfo> suppliesRealinfoList = suppliesRealinfoService.list(suppliesRealinfoQueryWrapper);
		for(SuppliesRealinfo realinfo:suppliesRealinfoList){
			realinfo.setIsNew(0L);
			suppliesRealinfoService.updateById(realinfo);
		}
		//新增物资状态
		info.setSuppliesId(supplies.getSuppliesId());
		info.setCreateTime(new Date());
		info.setRealInfo(jsonStr);
		info.setIsNew(1L);
		info.setIsDanger(0L);
		if(jsonStr.indexOf("气体浓度超标") > 0){
			info.setIsDanger(1L);
			//插入告警历史表
			SuppliesRealinfoWarning warning = new SuppliesRealinfoWarning();
			BeanUtils.copyProperties(info,warning);
			realinfoWarningMapper.insert(warning);
		}
		suppliesRealinfoService.save(info);
		//推送到webSocket
		if(webSocket.webSocketSet.size() >0){
			SuppliesRealinfoQuery query = new SuppliesRealinfoQuery();
			BeanUtils.copyProperties(info,query);
			query.setSuppliesName(supplies.getSuppliesName());
			String besnString = JSONObject.toJSONString(query);
			webSocket.GroupSending(besnString,"supplies");
		}
		return new ApiResult<>(Boolean.TRUE);
	}


}
